﻿using System;
using System.ServiceModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlueCurve.WcfServices
{
    /// <summary>
    /// Class de chargement de service wcf
    /// </summary>
    public static class Host
    {
        /// <summary>
        /// Instance du service
        /// </summary>
        private static ServiceHost myServiceHost = null;

        /// <summary>
        /// Demarre le service
        /// </summary>
        public static void StartService<T>()
        {
            myServiceHost = new ServiceHost(typeof(T));
            myServiceHost.Opened  += new EventHandler(myServiceHost_Opened);
            myServiceHost.Opening += new EventHandler(myServiceHost_Opening);
            myServiceHost.Faulted += new EventHandler(myServiceHost_Faulted);
            myServiceHost.Closing += new EventHandler(myServiceHost_Closing);
            myServiceHost.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(myServiceHost_UnknownMessageReceived);
            myServiceHost.Open();
        }

        /// <summary>
        /// Arrête le service
        /// </summary>
        public static void StopService()
        {
            //Call StopService from your shutdown logic (i.e. dispose method)
            if (myServiceHost.State != CommunicationState.Closed)
                myServiceHost.Close();
        }
        
        /// <summary>
        /// Affiche un message lors de la fermeture du service
        /// </summary>
        private static void myServiceHost_Closing(object sender, EventArgs e)
        {
            Console.WriteLine("{0}, sur l'ordinateur {1}", "Le service wcf est passé à l'état fermé", System.Net.Dns.GetHostName());
        }

        /// <summary>
        /// Affiche un message lors d'une erreur du service
        /// </summary>
        private static void myServiceHost_Faulted(object sender, EventArgs e)
        {
            Console.WriteLine("{0}, sur l'ordinateur {1}", "Le service wcf est passé à l'état faulted", System.Net.Dns.GetHostName());
        }

        /// <summary>
        /// Affiche un message quand un message non identifié à été reçus
        /// </summary>
        private static void myServiceHost_UnknownMessageReceived(object sender, UnknownMessageReceivedEventArgs e)
        {
            Console.WriteLine("{0}, sur l'ordinateur {1}, \n\n {2}", "Un message inconnu à été reçue par le service wcf ", System.Net.Dns.GetHostName(), "Message :" + e.Message);

        }

        /// <summary>
        /// Affiche un message lors de l'ouverture du service
        /// </summary>
        private static void myServiceHost_Opening(object sender, EventArgs e)
        {
            Console.WriteLine("{0}, sur l'ordinateur {1}", "Le service wcf est en cours de lancement", System.Net.Dns.GetHostName());
        }

        /// <summary>
        /// Affiche un message indiquant sur le service s'est ouvert correctement
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void myServiceHost_Opened(object sender, EventArgs e)
        {
            Console.WriteLine("{0}, sur l'ordinateur {1}", "Le service wcf est ouvert", System.Net.Dns.GetHostName());
        }
    }
}
